package com.java.basis.thread.n8.pool;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.wiring.BeanWiringInfo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * newCachedThreadPool 的队列 SynchronousQueue
 * 特点：
 * 	它没有容量，没有线程来取是放不进去的（一手交钱一手交货）
 *
 * @author wangning
 * @create 2022-02-09 16:32
 */
@Slf4j(topic = "c.TestThreadPoolExecutors02")
public class TestThreadPoolExecutors02 {
	public static void main(String[] args) throws InterruptedException {
		SynchronousQueue<Integer> integers = new SynchronousQueue<>();
		new Thread(() -> {
			try {
				log.debug("putting {}", 1);
				integers.put(1);
				log.debug("putted {}", 1);

				log.debug("putting {}", 2);
				integers.put(2);
				log.debug("putted {}", 2);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}, "t1").start();

		Thread.sleep(1000);

		new Thread(() -> {
			try {
				log.debug("taking {}", 1);
				integers.take();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}, "t2").start();

		Thread.sleep(1000);

		new Thread(() -> {
			try {
				log.debug("taking {}", 2);
				integers.take();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}, "t3").start();

	}


}
